package com.jone.phone.detail.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jone.phone.detail.R;
import com.jone.phone.detail.db.ConfigCenter;
import com.jone.phone.detail.util.Constants;
import com.mobclick.android.MobclickAgent;

public class ConfigView extends Activity {
	
	private static final int DIALOG_SET_TIME = 1;
	private static final int DIALOG_HELP = 2;
	private static final int DIALOG_SMS_WARN = 3;
	
	private TextView txtCallWarn;
	
	private TextView txtSmsWarn;
	
//	private CheckBox chkNotify;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.config);
        
        ImageView img1 = (ImageView)this.findViewById(R.id.tab1);
        ImageView img2 = (ImageView)this.findViewById(R.id.tab2);
        ImageView img3 = (ImageView)this.findViewById(R.id.tab3);
       
        img1.setOnClickListener(header);
        img2.setOnClickListener(header);
        img3.setOnClickListener(header);
        
//        chkNotify = (CheckBox)this.findViewById(R.id.enable_notify);
//        chkNotify.setChecked(ConfigCenter.getValue(this, Constants.ENABLE_TIME_NOTIFY, true));
        
        txtCallWarn = (TextView)this.findViewById(R.id.call_time_warn_desc);
        txtSmsWarn = (TextView)this.findViewById(R.id.sms_warn_desc);
    	String configTime = ConfigCenter.getValue(ConfigView.this, Constants.KEY_WARN_CALL_TIME, "0");
	   	String smsMax = ConfigCenter.getValue(ConfigView.this, Constants.KEY_WARN_SMS, "0");
	   	if((!"0".equals(configTime))&&(!"".equals(configTime))){	   		
	   		txtCallWarn.setText(getResources().getString(R.string.warn_desc) + configTime + getResources().getString(R.string.second));
	   	}
	   	if((!"0".equals(smsMax))&&(!"".equals(smsMax))){	   	
	   		txtSmsWarn.setText(getResources().getString(R.string.warn_desc) + smsMax + getResources().getString(R.string.sms_count));
	   	}  	
	   	
//	   	chkNotify.setOnClickListener(new View.OnClickListener() {			
//			@Override
//			public void onClick(View v) {				
//				ConfigCenter.setValue(ConfigView.this, Constants.ENABLE_TIME_NOTIFY, chkNotify.isChecked());
//			}
//		});
        
        LinearLayout setTime = (LinearLayout)this.findViewById(R.id.config_max_layout);
        LinearLayout setSms = (LinearLayout)this.findViewById(R.id.config_max_layout2);        
        LinearLayout helpLayout = (LinearLayout)this.findViewById(R.id.help_layoout);
        setTime.setOnClickListener(click);
        setSms.setOnClickListener(click);
        helpLayout.setOnClickListener(click);
        
	}
	
	@Override
    protected Dialog onCreateDialog(int id) {    	
		LayoutInflater factory = LayoutInflater.from(ConfigView.this);
	   	final View callTimeSetView = factory.inflate(R.layout.max_call_time, null);
	   	final View smsSetView = factory.inflate(R.layout.max_sms, null);	   	
	   	
	   	final EditText time = (EditText)callTimeSetView.findViewById(R.id.max_time_value);
	   	final EditText sms = (EditText)smsSetView.findViewById(R.id.sms_warn_value);
	   	String configTime = ConfigCenter.getValue(ConfigView.this, Constants.KEY_WARN_CALL_TIME, "0");
	   	String smsMax = ConfigCenter.getValue(ConfigView.this, Constants.KEY_WARN_SMS, "0");
	   	if((!"0".equals(configTime))&&(!"".equals(configTime))){
	   		time.setText(configTime);
	   		txtCallWarn.setText(getResources().getString(R.string.warn_desc) + configTime + getResources().getString(R.string.second));
	   	}
	   	if((!"0".equals(smsMax))&&(!"".equals(smsMax))){
	   		sms.setText(smsMax);
	   		txtSmsWarn.setText(getResources().getString(R.string.warn_desc) + smsMax + getResources().getString(R.string.sms_count));
	   	}  		   	
	   
        switch (id) {      
        case DIALOG_SET_TIME:	  
	        return new AlertDialog.Builder(ConfigView.this)
	//        .setIcon(R.drawable.alert_dialog_icon)
	        .setTitle(R.string.call_time_warn)
	        .setView(callTimeSetView)
	        .setPositiveButton(R.string.set, new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {	            	
	            	String timeVal = time.getText().toString()==""?"0":time.getText().toString();
	            	try{
	            		Integer.valueOf(timeVal);
	            		ConfigCenter.setValue(ConfigView.this, Constants.KEY_WARN_CALL_TIME, timeVal);	            	
		            	txtCallWarn.setText(getResources().getString(R.string.warn_desc) + timeVal + getResources().getString(R.string.second));
	            	}catch(Exception e){
	            		Toast.makeText(getApplicationContext(), "只能输入数字！", Toast.LENGTH_LONG).show();
	            	}
	            }
	        })
	        .setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                	ConfigCenter.setValue(ConfigView.this, Constants.KEY_WARN_CALL_TIME, "0");
                	time.setText("");
                	txtCallWarn.setText(getResources().getString(R.string.call_time_warn_desc));
                }
            })
	        .setNegativeButton(R.string.close, new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	
	                /* User clicked cancel so do some stuff */
	            }
	        })
	        .create();	
        case DIALOG_SMS_WARN:	  
	        return new AlertDialog.Builder(ConfigView.this)
	//        .setIcon(R.drawable.alert_dialog_icon)
	        .setTitle(R.string.sms_warn)
	        .setView(smsSetView)
	        .setPositiveButton(R.string.set, new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	            	String smsVal = sms.getText().toString()==""?"0":sms.getText().toString();
	            	try{
                		Integer.valueOf(smsVal);
                		ConfigCenter.setValue(ConfigView.this, Constants.KEY_WARN_SMS, smsVal);	            	
    	            	txtSmsWarn.setText(getResources().getString(R.string.warn_desc) + smsVal + getResources().getString(R.string.sms_count));
                	}catch(Exception e){
                		Toast.makeText(getApplicationContext(), "只能输入数字！", Toast.LENGTH_LONG).show();
                	}	            	
	            }
	        })
	        .setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                	
                	ConfigCenter.setValue(ConfigView.this, Constants.KEY_WARN_SMS, "0");
                	sms.setText("");
                	txtSmsWarn.setText(getResources().getString(R.string.sms_warn_desc));
                }
            })
	        .setNegativeButton(R.string.close, new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	
	                /* User clicked cancel so do some stuff */
	            }
	        })
	        .create();	
        case DIALOG_HELP:
        	return new AlertDialog.Builder(ConfigView.this)
            .setIcon(R.drawable.alert_dialog_icon)
            .setTitle(R.string.help)
            .setMessage(R.string.help_desc)
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked OK so do some stuff */
                }
            })  
            .create();
        }
        return null;
    }
	
	private OnClickListener header = new View.OnClickListener() {
 		
 		@Override
 		public void onClick(View v) {
 			switch(v.getId()){			
 			case R.id.tab1:
 				Intent i = new Intent(ConfigView.this,TodayView.class);
 				ConfigView.this.startActivity(i);
 				break;
 			case R.id.tab2:
				i = new Intent(ConfigView.this,StatView.class);
				ConfigView.this.startActivity(i);
				break; 			
 			}
 			
 		}
 	};
 	
 	private OnClickListener click = new View.OnClickListener() { 		
 		@Override
 		public void onClick(View v) {
 			switch(v.getId()){			
 			case R.id.config_max_layout:
 				showDialog(DIALOG_SET_TIME);
 				break;
 			case R.id.help_layoout:
 				showDialog(DIALOG_HELP);
				break; 	
 			case R.id.config_max_layout2:
 				showDialog(DIALOG_SMS_WARN);
 				break;
 			} 			
 		}
 	};
 	
 	public void onStop(){
    	this.finish();
    	super.onDestroy();
 	}
 	
 	public void onResume() {
 		  super.onResume();
 		  MobclickAgent.onResume(this);
 		}
 	
 	public void onPause() {
 		  super.onPause();
 		  MobclickAgent.onPause(this);
 		}

}
